package com.breeze.leetcode.middle;

import java.util.ArrayList;
import java.util.List;

/**
 * @url <a href="https://leetcode.cn/problems/maximum-distance-in-arrays/">624. 数组列表中的最大距离</a>
 * @author yangzh
 * @date 2025/2/19
 */
public class T624 {
    public static void main(String[] args) {
        List<List<Integer>> arrays = new ArrayList<List<Integer>>();
        arrays.add(List.of(4, 15));
        arrays.add(List.of(1, 20));
        T624 solution = new T624();
        int result = solution.maxDistance(arrays);
        System.out.println(result);
    }

    public int maxDistance(List<List<Integer>> arrays) {
        int res = 0;
        int n = arrays.get(0).size();
        int min_val = arrays.get(0).get(0);
        int max_val = arrays.get(0).get(n - 1);
        for (int i = 1; i < arrays.size(); i++) {
            n = arrays.get(i).size();
            res = Math.max(res, Math.max(Math.abs(arrays.get(i).get(n - 1) - min_val),
                    Math.abs(max_val - arrays.get(i).get(0))));
            min_val = Math.min(min_val, arrays.get(i).get(0));
            max_val = Math.max(max_val, arrays.get(i).get(n - 1));
        }
        return res;
    }
}
